package com.zycfc.zsf.boot.office.excel.service;

import com.zycfc.zsf.boot.office.excel.bean.*;
import com.zycfc.zsf.boot.office.excel.util.*;
import java.text.*;
import java.math.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import com.zycfc.zsf.boot.office.excel.constant.*;
import com.zycfc.zsf.boot.exception.*;
import java.io.*;
import java.util.*;
import org.slf4j.*;

public class ExcelImportService
{
    private static final Logger logger;
    private String columnType;
    private int skipRow;
    
    public ExcelImportService() {
        this.columnType = ExcelConstant.ExcelColumnTypeEnum.Type_String.getValue();
        this.skipRow = 1;
    }
    
    public ExcelImportResponse importExcel(final InputStream inputStream, final String excelType, final List<String> columnNames, final ExcelDataValidateService excelDataValidateService, final String validateType) {
        try {
            AssertUtils.isEmpty(columnNames, ExcelConstant.ExcelImportErrorEnum.ERROR2001.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2001.getDisplayName());
            AssertUtils.isNull(ExcelConstant.ExcelTypeEnum.getNm(excelType), ExcelConstant.ExcelImportErrorEnum.ERROR2002.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2002.getDisplayName());
            AssertUtils.isNull(ExcelConstant.ExcelDataValidateTypeEnum.getNm(validateType), ExcelConstant.ExcelImportErrorEnum.ERROR2006.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2006.getDisplayName());
            return this.createExcelDataList(inputStream, excelType, columnNames, null, this.skipRow, excelDataValidateService, validateType);
        }
        catch (ServiceException se) {
            return new ExcelImportResponse(se.getMessage());
        }
        catch (Exception e) {
            ExcelImportService.logger.error("importExcel:" + ExcelConstant.ExcelImportErrorEnum.ERROR2099.getValue(), (Throwable)e);
            return new ExcelImportResponse(ExcelConstant.ExcelImportErrorEnum.ERROR2099.getDisplayName());
        }
    }
    
    public ExcelImportResponse importExcel(final InputStream inputStream, final String excelType, final List<String> columnNames) {
        try {
            AssertUtils.isEmpty(columnNames, ExcelConstant.ExcelImportErrorEnum.ERROR2001.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2001.getDisplayName());
            AssertUtils.isNull(ExcelConstant.ExcelTypeEnum.getNm(excelType), ExcelConstant.ExcelImportErrorEnum.ERROR2002.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2002.getDisplayName());
            return this.createExcelDataList(inputStream, excelType, columnNames, null, this.skipRow, null, null);
        }
        catch (ServiceException se) {
            return new ExcelImportResponse(se.getMessage());
        }
        catch (Exception e) {
            ExcelImportService.logger.error("importExcel:" + ExcelConstant.ExcelImportErrorEnum.ERROR2099.getValue(), (Throwable)e);
            return new ExcelImportResponse(ExcelConstant.ExcelImportErrorEnum.ERROR2099.getDisplayName());
        }
    }
    
    public ExcelImportResponse importExcel(final InputStream inputStream, final String excelType, final List<String> columnNames, final List<String> columnTypes, final ExcelDataValidateService excelDataValidateService, final String validateType) {
        try {
            AssertUtils.isEmpty(columnNames, ExcelConstant.ExcelImportErrorEnum.ERROR2001.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2001.getDisplayName());
            AssertUtils.isNull(ExcelConstant.ExcelTypeEnum.getNm(excelType), ExcelConstant.ExcelImportErrorEnum.ERROR2002.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2002.getDisplayName());
            AssertUtils.isEmpty(columnTypes, ExcelConstant.ExcelImportErrorEnum.ERROR2003.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2003.getDisplayName());
            AssertUtils.isTrue(columnNames.size() != columnTypes.size(), ExcelConstant.ExcelImportErrorEnum.ERROR2005.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2005.getDisplayName());
            AssertUtils.isNull(ExcelConstant.ExcelDataValidateTypeEnum.getNm(validateType), ExcelConstant.ExcelImportErrorEnum.ERROR2006.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2006.getDisplayName());
            return this.createExcelDataList(inputStream, excelType, columnNames, columnTypes, this.skipRow, excelDataValidateService, validateType);
        }
        catch (ServiceException se) {
            return new ExcelImportResponse(se.getMessage());
        }
        catch (Exception e) {
            ExcelImportService.logger.error("importExcel:" + ExcelConstant.ExcelImportErrorEnum.ERROR2099.getValue(), (Throwable)e);
            return new ExcelImportResponse(ExcelConstant.ExcelImportErrorEnum.ERROR2099.getDisplayName());
        }
    }
    
    public ExcelImportResponse importExcel(final InputStream inputStream, final String excelType, final List<String> columnNames, final List<String> columnTypes) {
        try {
            AssertUtils.isEmpty(columnNames, ExcelConstant.ExcelImportErrorEnum.ERROR2001.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2001.getDisplayName());
            AssertUtils.isNull(ExcelConstant.ExcelTypeEnum.getNm(excelType), ExcelConstant.ExcelImportErrorEnum.ERROR2002.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2002.getDisplayName());
            AssertUtils.isEmpty(columnTypes, ExcelConstant.ExcelImportErrorEnum.ERROR2003.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2003.getDisplayName());
            AssertUtils.isTrue(columnNames.size() != columnTypes.size(), ExcelConstant.ExcelImportErrorEnum.ERROR2005.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2005.getDisplayName());
            return this.createExcelDataList(inputStream, excelType, columnNames, columnTypes, this.skipRow, null, null);
        }
        catch (ServiceException se) {
            return new ExcelImportResponse(se.getMessage());
        }
        catch (Exception e) {
            ExcelImportService.logger.error("importExcel:" + ExcelConstant.ExcelImportErrorEnum.ERROR2099.getValue(), (Throwable)e);
            return new ExcelImportResponse(ExcelConstant.ExcelImportErrorEnum.ERROR2099.getDisplayName());
        }
    }
    
    public ExcelImportResponse importExcel(final InputStream inputStream, final String excelType, final List<String> columnNames, final List<String> columnTypes, final int skipRow, final ExcelDataValidateService excelDataValidateService, final String validateType) {
        try {
            AssertUtils.isEmpty(columnNames, ExcelConstant.ExcelImportErrorEnum.ERROR2001.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2001.getDisplayName());
            AssertUtils.isNull(ExcelConstant.ExcelTypeEnum.getNm(excelType), ExcelConstant.ExcelImportErrorEnum.ERROR2002.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2002.getDisplayName());
            AssertUtils.isEmpty(columnTypes, ExcelConstant.ExcelImportErrorEnum.ERROR2003.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2003.getDisplayName());
            AssertUtils.isTrue(skipRow < 0, ExcelConstant.ExcelImportErrorEnum.ERROR2004.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2004.getDisplayName());
            AssertUtils.isTrue(columnNames.size() != columnTypes.size(), ExcelConstant.ExcelImportErrorEnum.ERROR2005.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2005.getDisplayName());
            AssertUtils.isNull(ExcelConstant.ExcelDataValidateTypeEnum.getNm(validateType), ExcelConstant.ExcelImportErrorEnum.ERROR2006.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2006.getDisplayName());
            return this.createExcelDataList(inputStream, excelType, columnNames, columnTypes, skipRow, excelDataValidateService, validateType);
        }
        catch (ServiceException se) {
            return new ExcelImportResponse(se.getMessage());
        }
        catch (Exception e) {
            ExcelImportService.logger.error("importExcel:" + ExcelConstant.ExcelImportErrorEnum.ERROR2099.getValue(), (Throwable)e);
            return new ExcelImportResponse(ExcelConstant.ExcelImportErrorEnum.ERROR2099.getDisplayName());
        }
    }
    
    public ExcelImportResponse importExcel(final InputStream inputStream, final String excelType, final List<String> columnNames, final List<String> columnTypes, final int skipRow) {
        try {
            AssertUtils.isEmpty(columnNames, ExcelConstant.ExcelImportErrorEnum.ERROR2001.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2001.getDisplayName());
            AssertUtils.isNull(ExcelConstant.ExcelTypeEnum.getNm(excelType), ExcelConstant.ExcelImportErrorEnum.ERROR2002.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2002.getDisplayName());
            AssertUtils.isEmpty(columnTypes, ExcelConstant.ExcelImportErrorEnum.ERROR2003.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2003.getDisplayName());
            AssertUtils.isTrue(skipRow < 0, ExcelConstant.ExcelImportErrorEnum.ERROR2004.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2004.getDisplayName());
            AssertUtils.isTrue(columnNames.size() != columnTypes.size(), ExcelConstant.ExcelImportErrorEnum.ERROR2005.getValue(), ExcelConstant.ExcelImportErrorEnum.ERROR2005.getDisplayName());
            return this.createExcelDataList(inputStream, excelType, columnNames, columnTypes, skipRow, null, null);
        }
        catch (ServiceException se) {
            return new ExcelImportResponse(se.getMessage());
        }
        catch (Exception e) {
            ExcelImportService.logger.error("importExcel:" + ExcelConstant.ExcelImportErrorEnum.ERROR2099.getValue(), (Throwable)e);
            return new ExcelImportResponse(ExcelConstant.ExcelImportErrorEnum.ERROR2099.getDisplayName());
        }
    }
    
    private ExcelImportResponse createExcelDataList(final InputStream inputStream, final String excelType, final List<String> columnNames, final List<String> columnTypes, final int skipRow, final ExcelDataValidateService excelDataValidateService, final String validateType) throws ServiceException {
        final List<LinkedHashMap<String, Object>> dataList = new ArrayList<LinkedHashMap<String, Object>>();
        List<Object> errorList = new ArrayList<Object>();
        final String errorInf = "\u7b2c{0}\u884c\u7b2c{1}\u5217\uff0c\u6570\u636e\u7c7b\u578b\u9519\u8bef\uff01";
        final Workbook workbook = this.getWorkbook(inputStream, excelType);
        final Sheet sheet = workbook.getSheetAt(0);
        final int firstRowIndex = sheet.getFirstRowNum();
        final int lastRowIndex = sheet.getLastRowNum();
        final int columnSize = columnNames.size();
        for (int i = firstRowIndex + skipRow; i <= lastRowIndex; ++i) {
            final Row row = sheet.getRow(i);
            final LinkedHashMap<String, Object> dataMap = new LinkedHashMap<String, Object>();
            for (int j = 0; j < columnSize; ++j) {
                String columntype = this.columnType;
                if (columnTypes != null && columnTypes.get(j) != null && !columnTypes.get(j).isEmpty()) {
                    columntype = columnTypes.get(j);
                }
                Object value = null;
                final Cell cell = row.getCell(j);
                if (ExcelConstant.ExcelColumnTypeEnum.Type_boolean.getValue().equals(columntype) || ExcelConstant.ExcelColumnTypeEnum.Type_Boolean.getValue().equals(columntype)) {
                    if (cell.getCellType() != 3) {
                        if (cell.getCellType() == 4) {
                            value = cell.getBooleanCellValue();
                        }
                        else if (cell.getCellType() != 3) {
                            errorList.add(MessageFormat.format(errorInf, i + 1, j + 1));
                        }
                    }
                }
                else if (ExcelConstant.ExcelColumnTypeEnum.Type_Date.getValue().equals(columntype)) {
                    if (cell.getCellType() == 0 || cell.getCellType() == 3) {
                        value = row.getCell(j).getDateCellValue();
                    }
                    else {
                        errorList.add(MessageFormat.format(errorInf, i + 1, j + 1));
                    }
                }
                else if (ExcelConstant.ExcelColumnTypeEnum.Type_int.getValue().equals(columntype) || ExcelConstant.ExcelColumnTypeEnum.Type_Integer.getValue().equals(columntype)) {
                    if (cell.getCellType() == 0) {
                        final BigDecimal num = new BigDecimal(cell.getNumericCellValue());
                        value = num.intValue();
                    }
                    else if (cell.getCellType() != 3) {
                        errorList.add(MessageFormat.format(errorInf, i + 1, j + 1));
                    }
                }
                else if (ExcelConstant.ExcelColumnTypeEnum.Type_long.getValue().equals(columntype) || ExcelConstant.ExcelColumnTypeEnum.Type_Long.getValue().equals(columntype)) {
                    if (cell.getCellType() == 0) {
                        final BigDecimal num = new BigDecimal(cell.getNumericCellValue());
                        value = num.longValue();
                    }
                    else if (cell.getCellType() != 3) {
                        errorList.add(MessageFormat.format(errorInf, i + 1, j + 1));
                    }
                }
                else if (ExcelConstant.ExcelColumnTypeEnum.Type_BigDecimal.getValue().equals(columntype)) {
                    if (cell.getCellType() == 0) {
                        value = cell.getNumericCellValue();
                    }
                    else if (cell.getCellType() != 3) {
                        errorList.add(MessageFormat.format(errorInf, i + 1, j + 1));
                    }
                }
                else if (ExcelConstant.ExcelColumnTypeEnum.Type_String.getValue().equals(columntype)) {
                    if (cell.getCellType() == 1 || cell.getCellType() == 3) {
                        value = cell.getStringCellValue();
                    }
                    else {
                        errorList.add(MessageFormat.format(errorInf, i + 1, j + 1));
                    }
                }
                else {
                    errorList.add(MessageFormat.format(errorInf, i + 1, j + 1));
                }
                dataMap.put(columnNames.get(j), value);
            }
            dataList.add(dataMap);
        }
        if (!errorList.isEmpty()) {
            return new ExcelImportResponse(null, errorList);
        }
        if (excelDataValidateService != null && validateType != null) {
            errorList = this.validateDatas(dataList, excelDataValidateService, validateType);
        }
        if (!errorList.isEmpty()) {
            return new ExcelImportResponse(null, errorList);
        }
        return new ExcelImportResponse(dataList);
    }
    
    private Workbook getWorkbook(final InputStream inputStream, final String excelType) throws ServiceException {
        try {
            return (Workbook)(ExcelConstant.ExcelTypeEnum.EXCEL2003.getValue().equals(excelType) ? new HSSFWorkbook(inputStream) : new XSSFWorkbook(inputStream));
        }
        catch (IOException e) {
            ExcelImportService.logger.error("getWorkbook:" + ExcelConstant.ExcelImportErrorEnum.ERROR2000.getValue(), (Throwable)e);
            throw new ServiceException((IExceptionCode)OfficeExceptionCode.ERROR2000);
        }
    }
    
    private List<Object> validateDatas(final List<LinkedHashMap<String, Object>> dataList, final ExcelDataValidateService excelDataValidateService, final String validateType) throws ServiceException {
        if (ExcelConstant.ExcelDataValidateTypeEnum.ALL.getValue().equals(validateType)) {
            return this.validateErrorAll(dataList, excelDataValidateService);
        }
        return this.validateErrorOne(dataList, excelDataValidateService);
    }
    
    private List<Object> validateErrorOne(final List<LinkedHashMap<String, Object>> dataList, final ExcelDataValidateService excelDataValidateService) throws ServiceException {
        final List<Object> errorList = new ArrayList<Object>();
        try {
            for (final LinkedHashMap<String, Object> dataMap : dataList) {
                final Object object = excelDataValidateService.validateRowData(dataMap);
                if (object != null) {
                    errorList.add(object);
                    break;
                }
            }
        }
        catch (Exception e) {
            ExcelImportService.logger.error("validateErrorOne:" + ExcelConstant.ExcelImportErrorEnum.ERROR2007.getValue(), (Throwable)e);
            throw new ServiceException((IExceptionCode)OfficeExceptionCode.ERROR2007);
        }
        return errorList;
    }
    
    private List<Object> validateErrorAll(final List<LinkedHashMap<String, Object>> dataList, final ExcelDataValidateService excelDataValidateService) throws ServiceException {
        final List<Object> errorList = new ArrayList<Object>();
        try {
            for (final LinkedHashMap<String, Object> dataMap : dataList) {
                final Object object = excelDataValidateService.validateRowData(dataMap);
                if (object != null) {
                    errorList.add(object);
                }
            }
        }
        catch (Exception e) {
            ExcelImportService.logger.error("validateErrorAll:" + ExcelConstant.ExcelImportErrorEnum.ERROR2007.getValue(), (Throwable)e);
            throw new ServiceException((IExceptionCode)OfficeExceptionCode.ERROR2007);
        }
        return errorList;
    }
    
    static {
        logger = LoggerFactory.getLogger((Class)ExcelImportService.class);
    }
}
